package com.kymatrix.mpo.plus.bpm.server.flowable.candidate.strategy;

import com.kymatrix.mpo.plus.bpm.server.flowable.candidate.BpmTaskCandidateStrategy;
import com.kymatrix.mpo.plus.bpm.server.flowable.enums.BpmTaskCandidateStrategyEnum;
import com.kymatrix.mpo.plus.system.api.service.UserApi;
import com.kymatrix.mpo.plus.system.api.dto.UserDto;
import com.kymatrix.mpo.plus.system.api.service.DeptApi;
import com.kymatrix.mpo.plus.common.util.StrUtils;
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;
import org.flowable.engine.delegate.DelegateExecution;
import org.springframework.stereotype.Component;

import static com.kymatrix.mpo.plus.common.util.CollectionUtils.convertSet;


/**
 * 部门的成员 {@link BpmTaskCandidateStrategy} 实现类
 *
 * @author kyle
 */
@Component
public class BpmTaskCandidateDeptMemberStrategy implements BpmTaskCandidateStrategy {

    @Resource
    private DeptApi deptApi;
    @Resource
    private UserApi userApi;

    @Override
    public BpmTaskCandidateStrategyEnum getStrategy() {
        return BpmTaskCandidateStrategyEnum.DEPT_MEMBER;
    }

    @Override
    public void validateParam(String param) {
        Set<String> deptIds = StrUtils.splitToStringSet(param);
        deptApi.validateDeptList(deptIds);
    }

    @Override
    public Set<String> calculateUsers(DelegateExecution execution, String param) {
        Set<String> deptIds = StrUtils.splitToStringSet(param);
        List<UserDto> users = userApi.getUserListByDeptIds(deptIds);
        return convertSet(users, UserDto::getId);
    }

}
